package com.gitee.aurora.server.service.impl;

import com.gitee.aurora.server.dao.PtConfigDao;

import com.gitee.aurora.server.service.PtConfigService;
import com.gitee.aurora.utils.PageParam;
import com.gitee.aurora.utils.Pagination;
import com.gitee.aurora.server.vo.PtConfig;
import com.gitee.aurora.server.vo.PtcjConfig;
import com.gitee.aurora.server.vo.SqlConfigItem;
import com.gitee.aurora.server.vo.TableConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PtConfigServiceImpl implements PtConfigService {

    @Autowired
    private PtConfigDao ptConfigDao;
    @Override
    public int insertPtConfigService(PtConfig ptConfig) {
        return ptConfigDao.insertPtConfigService(ptConfig);
    }

    @Override
    public int deletePtConfigService(PtConfig ptConfig) {
        return ptConfigDao.deletePtConfigService(ptConfig);
    }

    @Override
    public int updatePtConfigService(PtConfig ptConfig) {
        return ptConfigDao.updatePtConfigService(ptConfig);
    }

    @Override
    public Pagination queryPtConfigService(PtConfig ptConfig, PageParam pageParam) {
        return ptConfigDao.queryPtConfigService(ptConfig,pageParam);
    }

    @Override
    public List<SqlConfigItem> queryPtLx() throws Exception {
        return ptConfigDao.queryPtLx();
    }

    @Override
    public List<SqlConfigItem> queryPtXl() throws Exception {
        return ptConfigDao.queryPtXl();
    }

    @Override
    public List<TableConfig> queryTable() throws Exception {
        return ptConfigDao.queryTable();
    }



    @Override
    public List<PtcjConfig> queryPtcjConfig() throws Exception {
        List<PtcjConfig>pList=ptConfigDao.queryPtcjConfig();
        return pList.stream().filter(node -> node.getfCode()==null || "".equals(node.getfCode())).peek(tree -> tree.setChildList(getChildList(tree, pList))) .collect(Collectors.toList());
    }

    @Override
    public List<PtcjConfig> queryPtxjInfo(String sCode) throws Exception {
        return ptConfigDao.queryPtxjInfo(sCode);
    }

    @Override
    public List<PtConfig> querySql(PtConfig ptConfig) throws Exception {
        return ptConfigDao.querySql(ptConfig);
    }

    @Override
    public List<PtcjConfig> queryPtxjinfoList(PtcjConfig ptcjConfig) throws Exception {
        return ptConfigDao.queryPtxjinfoList(ptcjConfig);
    }


    private List<PtcjConfig> getChildList(PtcjConfig root, List<PtcjConfig> all) {
        return all.stream().filter(node -> Objects.equals(node.getfCode(), root.getsCode())).peek(node -> node.setChildList(getChildList(node, all))).collect(Collectors.toList());
    }
}
